#
#  Copyright 2011-2012 Gregory Banks
#
#  Licensed under the Apache License, Version 2.0 (the "License");
#  you may not use this file except in compliance with the License.
#  You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License.
#

platform_CFLAGS=    @platform_CFLAGS@
libxml_LIBS=	    @libxml_LIBS@
libbfd_LIBS=	    @libbfd_LIBS@

CC=		gcc
CDEBUGFLAGS=	-g
COPTFLAGS=	-O0
CCOVFLAGS=
ifneq ($(COVERAGE),)
CCOVFLAGS:=	--coverage
endif
CDEFINES=	$(platform_CFLAGS)
CWARNFLAGS=	-Wall -Wextra -Wno-unused-function -Wno-unused-parameter
CFLAGS=		$(CDEBUGFLAGS) $(COPTFLAGS) $(CWARNFLAGS) $(CCOVFLAGS) \
		$(CDEFINES) $(INCLUDES)

CXX=		g++
CXXFLAGS=	$(CFLAGS)

INCLUDES=	-I..
LIBS=		../libnovaprova.a -lstdc++ -ldl -lrt \
		$(libbfd_LIBS) $(libxml_LIBS)
DEPS=		../np.h ../libnovaprova.a

all install docs:

BASIC_TESTS= \
    tnassert \
    tnexit \
    tnfail \
    tnmemleak \
    tnna \
    tnpass \
    tnsegv \
    tnsigill \
    tnsyslog \
    tnnotests \
    tnnotests_param \
    tnnotests_fixture \
    tnnotests_mock \
    tnoverrun \
    tnuninit

BASIC_TESTS_CXX= \

OUTPUT_FORMATS= \
    junit

SIMPLE_TESTS= \
    tnaequalfail \
    tnaequalpass \
    tnafail \
    tnafalsefail \
    tnafalsepass \
    tnanequalfail \
    tnanequalpass \
    tnannullfail \
    tnannullpass \
    tnanullfail \
    tnanullpass \
    tnapass \
    tnapequalfail \
    tnapequalpass \
    tnapnequalfail \
    tnapnequalpass \
    tnasequalfail \
    tnasequalpass \
    tnasnequalfail \
    tnasnequalpass \
    tnatruefail \
    tnmocking \
    tnbug20 \
    tndynmock \
    tndynmock2 \
    tndynmock3 \
    tnparameter \
    tnsyslogmatch \
    tntimeout \
    tnfdleak \

SIMPLE_TESTS_CXX= \
    tnexcept \

PARALLEL_TESTS= \
    tnparallel \

PARALLELISM= \
    $(shell ./parallelism.sh)

MAINFUL_TESTS= \
    tfilename \
    tintercept \
    treader \
    tstack \

DUMPERS= \
    tdumpacu \
    tdumpafn \
    tdumpatype \
    tdumpdabbr \
    tdumpdfn \
    tdumpdstr \
    tdumpdvar \

COMPOUND_TESTS= \
    taddr2line \
    tinfo \
    $(DUMPERS) \

COMPOUND_SUBTESTS= \
    globfunc \
    membfunc \
    namespace \

COMPOUND_DATA= $(addprefix d-,$(COMPOUND_SUBTESTS))

TESTS= \
    $(SIMPLE_TESTS) \
    $(foreach t,$(BASIC_TESTS),$t $(foreach s,$(OUTPUT_FORMATS),$t%-f$s)) \
    $(MAINFUL_TESTS) \
    $(foreach t,$(COMPOUND_TESTS),$(foreach s,$(COMPOUND_DATA),$t%$s))

UNRELIABLE_TESTS= \
    $(foreach t,$(PARALLEL_TESTS),$t $(foreach j,$(PARALLELISM),$t%-j$j)) \

# Extract only the test executables actually mentioned in $TESTS
# which allows us to build only those executables actually needed
# to run the tests named in $TESTS.
TEST_EXES= $(sort $(foreach t,$(TESTS) $(UNRELIABLE_TESTS),$(firstword $(subst %,$(nul) $(nul),$t))))

BUILT_SCRIPTS=	$(addsuffix -normalize.pl,$(DUMPERS))

tests: $(TEST_EXES) $(BUILT_SCRIPTS) $(COMPOUND_DATA)

# Default to un-verbose
V=0

check: tests run

list:
	@for t in $(TESTS) ; do \
	    echo "$$t" | tr '%' ' ' ;\
	done

run: .announce-run $(addprefix .run%,$(TESTS))
run-unreliable: .announce-run $(addprefix .run%,$(UNRELIABLE_TESTS))

.PHONEY: .announce-run
.announce-run:
	@echo "=== Running tests"
	@echo "PARALLELISM=$(PARALLELISM)"

.run%:
	@[ "$V" -gt 0 ] && export VERBOSE=yes ; env bash runtest.sh $(wordlist 2,10,$(subst %,$(nul) $(nul),$@))

%: %.c fw.a fw.h $(DEPS)
	$(LINK.c) -o $@ $< fw.a $(LIBS)

%: %.cxx fw.a fw.h $(DEPS)
	$(LINK.C) -o $@ $< fw.a $(LIBS)

d-%: d-%.cxx
	$(LINK.C) $(CDEBUGFLAGS) -o $@ $<

%.c: %-genc.pl
	perl $< > $@

fw.a: fw.o fw-stubs.o
	ar ruv $@ fw.o fw-stubs.o
	ranlib $@

$(DUMPERS): tdump
	ln -f $< $@

$(addsuffix -normalize.pl,$(DUMPERS)): cat.pl
	ln -f $< $@

$(SIMPLE_TESTS) $(BASIC_TESTS) $(PARALLEL_TESTS): % : %.c $(DEPS)
	$(LINK.c) -o $@ $< $(LIBS)

$(SIMPLE_TESTS_CXX): % : %.cxx $(DEPS)
	$(LINK.C) -o $@ $< $(LIBS)

clean:
	$(RM) $(TEST_EXES) $(COMPOUND_DATA)
	$(RM) fw.a fw.o fw-stubs.o

distclean: clean
